$(function(){
    $('nav#menu_left').mmenu();
    var now = new Date();
    var optdatetime = {
        minDate: new Date(now.getFullYear(), now.getMonth(), now.getDate()),
        maxDate:new Date(2018, now.getMonth(), now.getDate()),
        showNow:false,
        preset: 'datetime',
        theme: 'android-ics light',
        lang: 'zh',
        display: 'modal',
        invalid: [ 'w0', 'w6', '5/1' ],
        mode: 'scroller',
        onSelect: function(time){
            var date = new Date(Date.parse(time.replace(/-/g, '/')));
            $('#startTime_show').val(date.format("Y年MM月dd日 hh点mm分")+' (周'+date.getZhDay(date)+')')
        }
    };
    $('#startTime').mobiscroll(optdatetime);
    $('#startTime_show').click(function(){
        $('#startTime').mobiscroll('show');
        return false;
    });
    // more
    $('#showMore').on('click', function(){
        var div = $('#more_info');
        if(div.css('display')==='none'){
            div.slideDown()
        }else{
            div.slideUp()
        }
    })
    // 提交
    $('#submit').on('click', function(){
        //console.log($('input[name="contact"]:checked').val())
        var btn = $(this);
        var actname = $.trim($('#actname').val()),
            startTime = $.trim($('#startTime').val()),
            actlocation = $.trim($('#actlocation').val()),
            remark = $.trim($('#remark').val()),
            factionId = $('#factionId').val(),
            hasContact = $('input[name="contact"]:checked').val(),
            hasBusiness = $('input[name="business"]:checked').val();
        if(actname === ''){
            $('#actname').parent().addClass('has-error');
            AlertBox.showTip('来个主题');
            return;
        }
        $('#actname').parent().removeClass('has-error')
        /*if(startTime === ''){
         $('#startTime').parent().addClass('has-error');
         AlertBox.showTip('什么时间呢?');
         return;
         }
         $('#startTime').parent().removeClass('has-error')
         if(actlocation === ''){
         $('#actlocation').parent().addClass('has-error');
         AlertBox.showTip('在哪里呢?');
         return;
         }
         $('#actlocation').parent().removeClass('has-error');*/
        btn.button('loading');
        $.post('/saveAct', {
            _csrf: $('#_csrf').val(),
            actname: actname,
            startTime: startTime,
            actlocation: actlocation,
            factionId: factionId,
            remark: remark,
            hasContact: hasContact || false,
            hasBusiness: hasBusiness || false
        }, function(res){
            if(res.error){
                AlertBox.showTip('出问题了...');
                btn.button('reset');
            }else{
                if(res.needalert){
                    return AlertBox.showConfirm('伙伴们已经入座？返回快乐邦公众号获取聚会暗号，让伙伴们对号入局～'
                        ,{ok:'获取暗号',cancel:'不需要'}, function(){
                            getJoinCode(res.joinCode, function(){
                                WeixinJSBridge.call('closeWindow');
                            })
                        },function(){
                            window.location.href = '/invite/'+res.data._id;
                        })
                }
                window.location.href = '/invite/'+res.data._id;
            }
        });
    })

});

function getJoinCode(code, cb){
    $.getJSON('/getJoinCode/'+code, {
        code: code
    }, function(res){
        cb();
    })
}